package diko_parent.eduservice.controller;


import com.diko.commonUtils.R;
import diko_parent.eduservice.entity.EduChapter;
import diko_parent.eduservice.entity.chapter.ChapterVo;
import diko_parent.eduservice.service.EduChapterService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Emma-Li
 * @since 2020-09-25
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu-chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    @ApiOperation(value = "嵌套章节数据列表")
    @GetMapping("getChapterByCourseId/{courseId}")
    public R getChapterByCourseId(
            @ApiParam(name="courseId",value = "课程ID",required = true)
            @PathVariable String courseId
    ){
        List<ChapterVo> chapter = chapterService.getAllChapter(courseId);
        return R.ok().data("items",chapter);
    }

    @ApiOperation(value = "新增章节")
    @PostMapping("chapter")
    public R save(
            @ApiParam(name="chapterVo",value = "章节对象",required = true)
            @RequestBody EduChapter chapter
            ){
        chapterService.save(chapter);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询章节")
    @GetMapping("getChapter/{id}")
    public R getById(
            @ApiParam(name="id",value = "章节ID",required = true)
            @PathVariable String id
    ){
        EduChapter chapter = chapterService.getById(id);
        return R.ok().data("item",chapter);
    }

    @ApiOperation(value = "根据ID修改章节")
    @PutMapping("updateChapter/{id}")
    public R updateById(
            @ApiParam(name="id",value = "章节ID",required = true)
            @PathVariable String id,

            //@ApiParam(name="chapter",value = "章节对象",required = true)
            EduChapter chapter
    ){
        chapter.setId(id);
        chapterService.updateById(chapter);
        return R.ok();
    }

    @ApiOperation(value = "根据ID删除章节")
    @DeleteMapping("deleteChapter/{id}")
    public R deleteById(
            @ApiParam(name="id",value = "章节ID",required = true)
            @PathVariable String id
    ){
        boolean result=chapterService.removeChapterById(id);
        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }
}

